/**
 * Created by Administrator on 2017-11-25
 */
var RESERVE;
var reservemesarr=[];
var reserveobj = {};
var cartobj = {};
define(function (require, exports, module) {
    'use strict';
  function shoplistinit() {
      $(".backcolor").click(function (event) {
          $("#goodspec").hide();
          $(".backcolor").hide();
          $("#bar").show();
      });
      $(document).click(function(){
          $(".select_module_con ul").slideUp();
      })
      var module=$(".select_result");
      module.click(function(e){
          var id = e.target.id;
          e.stopPropagation();
          var ul=$(this).next();
          ul.stop().slideToggle();
          ul.children().click(function(e){
              e.stopPropagation();
              var result = $(this).text();
              $('#'+id).text(result);
              ul.stop().slideUp();
          })
      })
      $(".spotlist").click(function () {
          $(".timeframe").hide();
      })
  }
    function choosespecs(id) {
        $("#goodspec").slideDown();
        $(".backcolor").css("opacity", "0.7");
        $(".backcolor").css("display", "block");
        $("#bar").css("display",'none');
        $.ajax({
            url: '/scenicspot/ticketdetail',
            type: 'GET',
            data: {
                id:id
            },
            success: function (res) {
                if (!res.success) {
                    alert(res.msg);
                    return;
                }else{
                    $('#ticketspec').html(res.data[0].description);
                    $('#tickettip').html(res.data[0].specialtip);
                    $('#ticketprice').html(res.data[0].price);
                    $('#summoney').html(res.data[0].price);
                    $(".reserve").click(function(){
                        // var pdate = $('#time_begin').val();
                        // var edate = $('#time_end').val();
                        // if(pdate==''||edate==''){
                        //     alert ('请选择完整开始和结束时间！');
                        //     return
                        // }
                        // var d = new Date;
                        // var today = new Date(d.getFullYear (), d.getMonth (), d.getDate (),d.getHours(),d.getMinutes());
                        // var reg = /\d+/g;
                        // var temp = pdate.match(reg);
                        // var tempE = edate.match(reg);
                        // var timeBegin = new Date (temp[0], parseInt (temp[1]) - 1, temp[2],temp[3],temp[4]);
                        // var timeEnd = new Date (tempE[0], parseInt (tempE[1]) - 1, tempE[2],tempE[3],tempE[4]);
                        // if (timeBegin <= today) {
                        //     alert ('请选择开始时间为当前时间或以后！');
                        // }else if(timeEnd<=timeBegin) {
                        //     alert ('请选择结束时间大于等于开始时间！');
                        // }
                        // else{
                        SHOP.reserve(res.data);
                        // }
                    });
                    $(".cart").click(function(){
                        // var pdate = $('#time_begin').val();
                        // var edate = $('#time_end').val();
                        // if(pdate==''||edate==''){
                        //     alert ('请选择完整开始和结束时间！');
                        //     return
                        // }
                        // var d = new Date;
                        // var today = new Date(d.getFullYear (), d.getMonth (), d.getDate ());
                        // var reg = /\d+/g;
                        // var temp = pdate.match(reg);
                        // var tempE = edate.match(reg);
                        // var timeBegin = new Date (temp[0], parseInt (temp[1]) - 1, temp[2],temp[3],temp[4]);
                        // var timeEnd = new Date (tempE[0], parseInt (tempE[1]) - 1, tempE[2],tempE[3],tempE[4]);
                        // if (timeBegin <= today) {
                        //     alert ('请选择开始时间大于当前时间！');
                        // }else if(timeEnd<=timeBegin) {
                        //     alert ('请选择结束时间大于开始时间！');
                        // }else{
                        SHOP.addcart(res.data[0]);
                        // }
                    });
                }
            }
        })
    }

    function timechoose() {
        // var currYear = (new Date()).getFullYear();
        // var opt={};
        // opt.date = {preset : 'date'};
        // opt.datetime = {preset : 'datetime'};
        // opt.time = {preset : 'time'};
        // opt.default = {
        //     theme: 'android-ics light', //皮肤样式
        //     display: 'modal', //显示方式
        //     mode: 'scroller', //日期选择模式
        //     lang:'zh',
        //     startYear:currYear - 10, //开始年份
        //     endYear:currYear + 10 //结束年份
        // };
        // var optDateTime = $.extend(opt['datetime'], opt['default']);
        // $("#time_begin").mobiscroll(optDateTime).datetime(optDateTime);
        // $("#time_end").mobiscroll(optDateTime).datetime(optDateTime);
    }

    function addshoplist() {
        var tickername = $('#tickername').val();
        var ticketdes = $('#ticketdes').val();
        var ticketprice = $('#ticketprice').val();
        var tickettype = $('#tickettype').val();
        var specialtip = $('#specialtip').val();
        $.ajax({
            url: '/scenicspot/addticketlist',
            type: 'POST',
            data: {
                tickername:tickername,
                ticketdes:ticketdes,
                ticketprice:ticketprice,
                tickettype:tickettype,
                specialtip:specialtip,
                userid:user.id
            },
            success: function (res) {
                if (!res.success) {
                    alert(res.msg);
                    return;
                }else{
                    alert('成功!')
                }
            }
        })
    }

    function reserve(data) {
        reservearray = [];
        var count = $('#count').text();
        var price=$('#ticketprice').text();
        var allmoney = parseInt(count)*parseInt(price);
        var timesinchoose = $('#timesinchoose').val();
        var timemulchoose = $('#timemulchoose').val();
        var starttime = '';
        var endtime = '';
        if (timesinchoose) {
            starttime = new Date(timesinchoose);
        } else if(timemulchoose) {
            var timechoose = timemulchoose.split('-');
            starttime = new Date(timechoose[0]);
            endtime = new Date(timechoose[1]);
        }
        for(var i=0;i<data.length;i++){
            data[i].starttime = starttime;
            data[i].userid = user.id;
            data[i].endtime = endtime;
            data[i].count = count;
            data[i].price=price;
            data[i].deposit=parseFloat(allmoney)*0.1
        }
        if(count>0){
            var ordermes = {
                id:data[0].id,
                starttime:starttime,
                endtime:endtime,
                userid:user.id,
                price:price,
                count:count,
                deposit:parseFloat(allmoney)*0.1,
                spotid:data[0].spotid,
                description:data[0].description,
                specialtip:data[0].specialtip,
                name:data[0].name,
                type:data[0].type,
                goodimg:data[0].goodimg
            }
            reservearray[0] = ordermes;
        }
        reserveobj = {
            name:data[0].name,
            userid:user.id,
            img:data[0].goodimg,
            price:price,
            count:count,
            deposit:parseFloat(allmoney)*0.1
        }
        seajs.use("/script/shoppingcart/comfirmorder", function (comfirm) {
            $("#scenicbody").html(new EJS({url: "/views/shoppingcart/comfirmorder.ejs"}).render({
                data: data,
                count:count
            }));
            COMFIRMORDER = comfirm;
        });

        // seajs.use("/script/scenicspot/reserve",function(reserve){
        //     $("#scenicbody").html(new EJS({url: "/views/scenicspot/reserve.ejs"}).render({
        //         reservemesarr:reservemesarr,
        //         numberall:count,
        //         allmoney:allmoney
        //     }));
        //     RESERVE = reserve;
        // });
    }

    function count(type) {
        var count =$(".count").text();
        console.log(count);
        if(type =='jian'){
            count--;
            if(count<0){
                count=0;
            }
            $(".count").html(count);

        }else {
            count++;
            $(".count").html(count);

        }
        pricechage();

    }

    function pricechage() {
        var count=$('#count').text();
        var price=$('#ticketprice').text();
        var summoney = count*parseInt(price);
        $('#summoney').html(summoney);

    }

    function addcart(data) {
        var count = $('#count').html();
        console.log(count);
        var timesinchoose = $('#timesinchoose').val();
        var timemulchoose = $('#timemulchoose').val();
        var starttime = '';
        var endtime = '';
        if (timesinchoose) {
            starttime = new Date(timesinchoose);
        } else if(timemulchoose) {
            var timechoose = timemulchoose.split('-');
            starttime = new Date(timechoose[0]);
            endtime = new Date(timechoose[1]);
        }
        cartobj = {
            id:'',
            status:'',
            spotid:data.spotid,
            specialtip:data.specialtip,
            description:data.description,
            goodimg:data.goodimg,
            name:data.name,
            type:data.type,
            gooddetailid:data.id,
            userid:user.id,
            count:count,
            price:data.price,
            starttime:starttime,
            endtime:endtime
        }
        $.ajax({
            url: '/scenicspot/savetocart',
            type: 'POST',
            data: {
                cartobj:cartobj
            },
            success: function (res) {
                if (!res.success) {
                    alert(res.msg);
                    return;
                }else{
                    toast.show('已保存到购物车!');
                    $("#goodspec").hide();
                    $(".backcolor").hide();
                    $("#bar").show();
                }
            }
        })


    }
    return {
        choosespecs:choosespecs,
        // adddel:adddel,
        reserve:reserve,
        addcart:addcart,
        addshoplist:addshoplist,
        timechoose:timechoose,
        pricechage:pricechage,
        count:count,
        shoplistinit:shoplistinit
    }
})